package com.byt.user.viewmodel;

import com.byt.base.activity.IBaseView;
import com.byt.base.model.BaseModel;
import com.byt.base.viewmodel.BaseViewModel;
import com.byt.common.bean.VersionInfo;
import com.byt.netapi.boby.FormBodys;
import com.byt.user.bean.LoginBean;
import com.byt.user.model.LoginModel;

import java.util.Map;

public class LoginViewModel extends BaseViewModel<LoginViewModel.LoginView, LoginModel> implements LoginModel.ILoginLister<VersionInfo> {
    public LoginViewModel(Map<String, Object> map) {
        model = new LoginModel(map);
        model.register(this);
        model.getCachedDataAndLoad();
    }

    @Override
    public void onLogin(LoginBean loginBean) {
        if (getPageView() != null) {
            getPageView().loginSuccess(loginBean);
        }
    }

    @Override
    public void getCode(String message, String code) {
        if (getPageView() != null) {
            getPageView().onCode(message,code);
        }
    }

    @Override
    public void onLoadFinish(BaseModel model, VersionInfo data) {
        if (getPageView() != null) {
            getPageView().onUpdateApp(data);
        }
    }

    @Override
    public void onLoadFail(BaseModel model, String msg) {
        if (getPageView() != null) {
            getPageView().onRefreshFailure(msg);
        }
    }

    public void loginUser(FormBodys formBodys){
        model.onLogin(formBodys);
    }

    public void loginCode(FormBodys formBodys){
        model.onLogin(formBodys);
    }

    public void getCode(FormBodys formBodys){
        model.getCode(formBodys);
    }

    public interface LoginView extends IBaseView {
        void onUpdateApp(VersionInfo versionInfo);

        void loginSuccess(LoginBean loginBean);

        void onCode(String message, String code);
    }
}
